package example;

import example.entity.Fine;
import example.entity.User;
import example.service.FineService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * 罚款管理
 *
 * @author 张心怡
 * @date 2024/12/16
 */
@WebServlet("/fineServlet")
public class FineServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("path");
        if (action == null) {
            action = "list";
        }
        switch (action) {
            case "list":
                User user = (User) request.getSession().getAttribute("user");
                List<Fine> finesAll = null;
                if (user.getType() == 1) {
                    finesAll = FineService.getFinesAll();
                } else {
                    finesAll = FineService.getFinesByUserId(user.getId());
                }
                request.setAttribute("fines", finesAll);
                request.getRequestDispatcher("manageFines.jsp").forward(request, response);
                break;
            case "del":
                FineService.delFine(Integer.parseInt(request.getParameter("id")));
                response.sendRedirect("fineServlet");
                break;
            case "payFines":
                FineService.payFine(Integer.parseInt(request.getParameter("id")));
                response.sendRedirect("fineServlet");
                break;
        }
    }
}
